<template>
	<view class="page-body">
		<!-- 页面内容开始 -->

		<!-- 表格搜索组件开始 -->
		<vk-data-table-query
			v-model="queryForm1.formData"
			:columns="queryForm1.columns"
			:main-columns="['position','nickname','remark']"
			size="mini"
			@search="search"
		></vk-data-table-query>
		<!-- 表格搜索组件结束 -->

		<!-- 自定义按钮区域开始 -->
		<view>
			<el-row>
				<el-button type="success" size="mini" icon="el-icon-circle-plus-outline" @click="addBtn">添加代理</el-button>
			</el-row>
		</view>
		<!-- 自定义按钮区域结束 -->

		<!-- 表格组件开始 -->
		<vk-data-table
			ref="table1"
			:action="table1.action"
			:columns="table1.columns"
			:query-form-param="queryForm1"
			:right-btns="['detail_auto','update','delete']"
			:selection="false"
			:row-no="true"
			:pagination="true"
			size="mini"
			@update="updateBtn"
			@delete="deleteBtn"
			@current-change="currentChange"
			@selection-change="selectionChange"
		></vk-data-table>
		<!-- 表格组件结束 -->

		<!-- 添加或编辑的弹窗开始 -->
		<vk-data-dialog
			v-model="form1.props.show"
			:title="form1.props.title"
			width="600px"
			mode="form"
			:close-on-click-modal="false"
		>
			<vk-data-form
				v-model="form1.data"
				:rules="form1.props.rules"
				:action="form1.props.action"
				:form-type="form1.props.formType"
				:columns='form1.props.columns'
				label-width="100px"
				style="user-select:none;"
				@success="form1.props.show = false;refresh();"
			>
				<template v-slot:reward_rule="{ form, keyName }">
					<view>
						<text style="margin-right: 20px;">是否启用</text>
						<el-switch v-model="form[keyName].is_calc" :active-value="true" :inactive-value="false" active-color="#13ce66" inactive-color="#ff4949"></el-switch>
						<view v-if="form[keyName].is_calc" style="display:grid; grid-template-columns: 100px 360px;align-items: center;line-height: 40px;">
							<view>奖励计算方式</view>
							<vk-data-input-radio
							  v-model="form[keyName].is_fixed_calc"
								optionType="button"
								size="mini"
							  :localdata="[
							    { value:false, label:'按百分比' },
							    { value:true, label:'按单品个数' }
							  ]"
							></vk-data-input-radio>
							<view>奖励系数</view>
							<vk-data-input-money v-if="form[keyName].is_fixed_calc" v-model="form[keyName].fixed_value" size="mini" placeholder="请输入每个产品的奖励(元)" width="220px" :clearable="false" rightText="元"></vk-data-input-money>
							<vk-data-input-percentage v-else size="mini" v-model="form[keyName].proportion_value" placeholder="请输入奖励百分比" :precision="2" :max="100" width="220px" :clearable="false"></vk-data-input-percentage>
						</view>
					</view>
				</template>
			</vk-data-form>
		</vk-data-dialog>
		<!-- 添加或编辑的弹窗结束 -->
		<!-- 页面内容结束 -->
	</view>
</template>

<script>
	var that;													// 当前页面对象
	var vk = uni.vk;									// vk实例
	var originalForms = {};						// 表单初始化数据


	export default {
		data() {
			// 页面数据变量
			return {
				// 页面是否请求中或加载中
				loading:false,
				// init请求返回的数据
				data:{

				},
				// 表格相关开始 -----------------------------------------------------------
				table1:{
					// 表格数据请求地址
					action:"admin/business/sys.fxRegionalAgent.getList",
					// 表格字段显示规则
					columns:[
						{ key:"position", title:"代理区域", type:"address", minWidth:250 },
						{
							key:"lv", title:"级别", type:"radio", width:80, sortable:"custom",
							data:[
								{ value:1, label:"省级代理" },
								{ value:2, label:"市级代理" },
								{ value:3, label:"区级代理" }
							]
						},
						{ key:"userInfo.avatar", title:"代理头像", type:"avatar", width:80 },
						{ key:"userInfo.nickname", title:"代理昵称", type:"text", minWidth:120 },
						{ key:"remark", title:"备注", type:"text", width:140 },
						{ key:"reward_rule", title:"奖励规则", type:"html", width:120,
							formatter: function(val, row, column, index) {
								let str = ``;
								if(val.is_calc){
									if(val.is_fixed_calc){
										str = `单品 * ${parseFloat(vk.pubfn.priceFilter(val.fixed_value))} 元`;
									}else{
										str = `实付金额 * ${vk.pubfn.percentageFilter(val.proportion_value)}`
									}
								}else{
									str = "无奖励";
								}
								return str;
							}
						},
						{ key:"add_time", title:"代理时间", type:"time", width:120, valueFormat:"yyyy-MM-dd", sortable:"custom" },
						{ key:"exp_time", title:"到期时间", type:"time", width:120, valueFormat:"yyyy-MM-dd", defaultValue: "永久", sortable:"custom" },
						{ key:"exp_time", title:"到期剩", type:"dateDiff2", endText:"已到期", width:80, defaultValue: "永久", sortable:"custom" },
					],
					// 多选框选中的值
					multipleSelection:[],
					// 当前高亮的记录
					selectItem:""
				},
				// 表格相关结束 -----------------------------------------------------------
				// 表单相关开始 -----------------------------------------------------------
				// 查询表单请求数据
				queryForm1:{
					// 查询表单数据源，可在此设置默认值
					formData:{

					},
					// 查询表单的字段规则 fieldName:指定数据库字段名,不填默认等于key
					columns:[
						{ key:"position", title:"省", type:"address", width:120, mode:"=", level:1 },
						{ key:"position", title:"省市", type:"address", width:120, mode:"=", level:2 },
						{ key:"position", title:"省市区", type:"address", width:120, mode:"=", level:3 },
						{ key:"nickname", title:"代理者昵称", type:"text", width:160, mode:"%%", fieldName:"userInfo.nickname", lastWhereJson:true },
						{ key:"remark", title:"备注", type:"text", width:160, mode:"%%" },
						{ key:"add_time", title:"代理时间", type:"datetimerange", width:350, mode:"[]" },
						{ key:"exp_time", title:"到期时间", type:"datetimerange", width:350, mode:"[]" },
					]
				},
				form1:{
					// 表单请求数据，此处可以设置默认值
					data: {
						user_id:"",
						position:{},
						lv:0,
						add_time:"",
						reward_rule:{
							is_calc:true,
							is_fixed_calc:false
						},
						check_repeat:true
					},
					// 表单属性
					props: {
						// 表单请求地址
						action:"",
						// 表单字段显示规则
						columns:[
							{
								key: "user_id", title: "选择用户", type: "table-select", placeholder:"请选择用户",
								action:"admin/business/sys.user.getList",
								formData:{
									is_fx:1
								},
								columns:[
									{ key:"avatar", title:"头像", type:"avatar", width:60 },
									{ key:"nickname", title:"昵称", type:"text", nameKey:true },
									{ key:"mobile", title:"手机", type:"text" },
									{ key:"comment", title:"备注", type:"text" },
									{ key:"_id", title:"角色标识", type:"text", idKey:true, show:["none"] }, // idKey:true 代表此字段为主键字段，若设置show:["none"],则可以在表格中隐藏该字段的显示
								],
								queryColumns:[
									{ key:"nickname", title:"昵称", type:"text", width:160, mode:"%%" },
									{ key:"mobile", title:"手机号", type:"text", width:160, mode:"%%", lastWhereJson: true },
									{ key:"comment", title:"备注", type:"text", width:160, mode:"%%" },
									{ key:"is_fx", title:"状态", type:"number", width:160, mode:"=", fieldName:"fx.is_fx", show:["none"] },
								],
								disabled:["update"]
							},
							{ key:"remark", title:"备注", type:"text" },
							{
							  key:"lv", title:"等级", type:"radio",
								optionType:"button",
							  data:[
							    { value:1, label:"省代" },
							    { value:2, label:"市代" },
									{ value:3, label:"区代" }
							  ],
								disabled:["update"]
							},
							{ key:"position", title:"代理省", type:"address", level:1, showRule:"lv=1", disabled:["update"] },
							{ key:"position", title:"代理市", type:"address", level:2, showRule:"lv=2", disabled:["update"] },
							{ key:"position", title:"代理区", type:"address", level:3, showRule:"lv=3", disabled:["update"] },
							{ key:"add_time", title:"起始时间", type:"date" },
							{ key:"exp_time", title:"截止时间", type:"date" },
							{ key:"check_repeat", title:"检查重复", type:"radio",
								optionType:"button",
								size:"mini",
								data:[
									{ value:true, label:"检查重复" },
									{ value:false, label:"不检查重复" }
								]
							},
							{ key:"reward_rule", title:"奖励规则", type:"object" }
						],
						// 表单验证规则
						rules:{
							user_id: [
								{ required: true, message: '请选择用户', trigger: 'blur' },
							],
							lv: [
								{ required: true, message: '请选择等级', trigger: 'blur' },
								// 自定义
								{ validator: function(rule, value, callback){
									if (typeof value !== "number") {
										return callback(new Error('请选择等级'));
									} else if (value <= 0) {
										return callback(new Error('请选择等级'));
									} else {
										return callback();
									}
								}, trigger: 'blur' }
							],
							position: [
								{ required: true, message: '请选择省市区', trigger: 'blur' },
							],
							add_time: [
								{ required: true, message: '起始时间不能为空', trigger: 'blur' },
							],
							reward_rule: [
								{ required: true, message: '请正确填写奖励规则', trigger: 'blur' },
								// 自定义
								{ validator: function(rule, value, callback){
									if(value.is_calc && value.is_fixed_calc && !value.fixed_value){
										return callback(new Error('请正确填写奖励规则'));
									}else if(value.is_calc && !value.is_fixed_calc && !value.proportion_value){
										return callback(new Error('请正确填写奖励规则'));
									}else{
										return callback();
									}
								}, trigger: 'blur' }
							]
						},
						// add 代表添加 update 代表修改
						formType:"",
						// 是否显示表单的弹窗
						show:false
					}
				},
				// 其他弹窗表单
				formDatas:{},
				// 表单相关结束 -----------------------------------------------------------
			};
		},
		// 监听 - 页面每次【加载时】执行(如：前进)
		onLoad(options = {}) {
			that = this;
			vk = that.vk;
			that.options = options;
			that.init(options);
		},
		// 监听 - 页面【首次渲染完成时】执行。注意如果渲染速度快，会在页面进入动画完成前触发
		onReady() {},
		// 监听 - 页面每次【显示时】执行(如：前进和返回) (页面每次出现在屏幕上都触发，包括从下级页面点返回露出当前页面)
		onShow() {},
		// 监听 - 页面每次【隐藏时】执行(如：返回)
		onHide() {},
		// 函数
		methods: {
			// 页面数据初始化函数
			init(options) {
				originalForms["form1"] = vk.pubfn.copyObject(that.form1);
			},
			// 页面跳转
			pageTo(path) {
				vk.navigateTo(path);
			},
			// 表单重置
			resetForm(){
				vk.pubfn.resetForm(originalForms, that);
			},
			// 搜索
			search(){
				that.$refs.table1.search();
			},
			// 刷新
			refresh(){
				that.$refs.table1.refresh();
			},
			// 获取当前选中的行的数据
			getCurrentRow(){
				return that.$refs.table1.getCurrentRow();
			},
			// 监听 - 行的选中高亮事件
			currentChange(val){
				that.table1.selectItem = val;
			},
			// 当选择项发生变化时会触发该事件
			selectionChange(list) {
				that.table1.multipleSelection = list;
			},
			// 显示添加页面
			addBtn(){
				that.resetForm();
				that.form1.props.action = 'admin/business/sys.fxRegionalAgent.add';
				that.form1.props.formType = 'add';
				that.form1.props.title = '添加';
				that.form1.props.show = true;
			},
			// 显示修改页面
			updateBtn({ item }){
				that.form1.props.action = 'admin/business/sys.fxRegionalAgent.update';
				that.form1.props.formType = 'update';
				that.form1.props.title = '编辑';
				that.form1.props.show = true;
				that.form1.data = item;
			},
			// 删除按钮
			deleteBtn({ item, deleteFn }){
				deleteFn({
					action:"admin/business/sys.fxRegionalAgent.delete",
					data:{
						_id: item._id,
						b_user_id: item.b_user_id
					},
				});
			},
			// 监听 - 批量操作的按钮点击事件
			batchBtn(index){
				switch(index){
					case 1: vk.toast("批量操作按钮1"); break;
					case 2: vk.toast("批量操作按钮2"); break;
					default : break;
				}
			}
		},
		// 监听属性
		watch: {

		},
		// 计算属性
		computed: {

		}
	};
</script>
<style lang="scss" scoped>
	.page-body {

	}

</style>
